/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package src.com.apresentacao.pedagogico;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.plaf.basic.BasicTabbedPaneUI;
import src.com.negocio.pedagogico.CadastrarIdioma;

/**
 *
 * @author Giane
 */
public class CadastroIdioma extends javax.swing.JFrame {

    /**
     * Creates new form CadastroIdioma
     */
    private ArrayList<String> b = new ArrayList<String>();

    public CadastroIdioma() throws SQLException {
        initComponents();
        CadastrarIdioma c = new CadastrarIdioma();

        b = c.consultaIdioma();
        for (int i = 0; i < b.size(); i++) {
            tabIdioma.setValueAt(b.get(i), i, 1);
            //System.out.print(c.consultaIdioma().get(i));            
        }

    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jInternalFrame1 = new javax.swing.JInternalFrame();
        jScrollPane1 = new javax.swing.JScrollPane();
        tabIdioma = new javax.swing.JTable();
        salvar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jInternalFrame1.setTitle("Cadastro - Idioma");
        jInternalFrame1.setVisible(true);

        tabIdioma.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {"1", null},
                {"2", null},
                {"3", null},
                {"4", null},
                {"5", null},
                {"6", ""},
                {"7", null},
                {"8", null},
                {"9", null},
                {"10", null}
            },
            new String [] {
                "", "Idioma"
            }
        ));
        jScrollPane1.setViewportView(tabIdioma);
        if (tabIdioma.getColumnModel().getColumnCount() > 0) {
            tabIdioma.getColumnModel().getColumn(0).setMinWidth(30);
            tabIdioma.getColumnModel().getColumn(0).setPreferredWidth(30);
            tabIdioma.getColumnModel().getColumn(0).setMaxWidth(30);
        }

        salvar.setText("Salvar");
        salvar.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                salvarMouseClicked(evt);
            }
        });

        javax.swing.GroupLayout jInternalFrame1Layout = new javax.swing.GroupLayout(jInternalFrame1.getContentPane());
        jInternalFrame1.getContentPane().setLayout(jInternalFrame1Layout);
        jInternalFrame1Layout.setHorizontalGroup(
            jInternalFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jInternalFrame1Layout.createSequentialGroup()
                .addContainerGap(11, Short.MAX_VALUE)
                .addGroup(jInternalFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jInternalFrame1Layout.createSequentialGroup()
                        .addComponent(salvar)
                        .addGap(19, 19, 19))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jInternalFrame1Layout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 375, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap())))
        );
        jInternalFrame1Layout.setVerticalGroup(
            jInternalFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jInternalFrame1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 188, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 42, Short.MAX_VALUE)
                .addComponent(salvar)
                .addGap(7, 7, 7))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jInternalFrame1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jInternalFrame1)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void salvarMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_salvarMouseClicked

        ArrayList<String> idiomas = new ArrayList<String>();
        CadastrarIdioma c = new CadastrarIdioma();

        for (int i = 0; i < tabIdioma.getRowCount(); i++) {
            if (tabIdioma.getValueAt(i, 1) != null) {
                idiomas.add(tabIdioma.getValueAt(i, 1).toString());
            }
        }

        //System.out.println("idiomas" + idiomas);
        //System.out.println("base" + b);


        ArrayList<String> result = new ArrayList<String>();
        result.clear();
        int u = 0;
        for (int i = 0; i < idiomas.size(); i++) {
            if (b.contains(idiomas.get(i))) {
                u++;
            } else {
                //System.out.print(0);
                result.add(idiomas.get(i));
                javax.swing.JDialog inf = new javax.swing.JDialog();
            inf.setSize(250, 150);
            javax.swing.JOptionPane.showMessageDialog(inf, "O idioma cadastrado com sucesso!", "Informação", javax.swing.JOptionPane.INFORMATION_MESSAGE);             
            }
        }
        

        if (u == idiomas.size()) {
            javax.swing.JDialog inf = new javax.swing.JDialog();
            inf.setSize(250, 150);
            javax.swing.JOptionPane.showMessageDialog(inf, "O idioma informado já esta cadastrado!", "Informação", javax.swing.JOptionPane.WARNING_MESSAGE);
            
        }
        u = 0;
        

        try {
            c.insereIdioma(result);
        } catch (SQLException ex) {
            Logger.getLogger(CadastroIdioma.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_salvarMouseClicked

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(CadastroIdioma.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(CadastroIdioma.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(CadastroIdioma.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(CadastroIdioma.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    new CadastroIdioma().setVisible(true);
                } catch (SQLException ex) {
                    Logger.getLogger(CadastroIdioma.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JInternalFrame jInternalFrame1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton salvar;
    private javax.swing.JTable tabIdioma;
    // End of variables declaration//GEN-END:variables
}
